//
//  ReadListViewController.m
//  Leisure
//
//  Created by Alex on 16/6/29.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ReadListViewController.h"

@interface ReadListViewController ()<UITableViewDataSource,UITableViewDelegate>
// 分段视图控制器
@property (nonatomic,strong)UISegmentedControl *segentedmControl;
// 最新列表
@property (weak, nonatomic) IBOutlet UITableView *newestTableView;
// 最热列表
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;
// 滚动视图
@property (weak, nonatomic) IBOutlet UIScrollView *scroll;
// 最新列表的数据起始位置
@property (nonatomic,assign)NSInteger newStart;
// 最新列表的数据条数
@property (nonatomic,assign)NSInteger newLimit;
// 最热列表的数据起始位置
@property (nonatomic,assign)NSInteger hotStart;
// 最热列表的数据条数
@property (nonatomic,assign)NSInteger hotLimit;
// 最新列表数据源
@property (nonatomic,strong)NSMutableArray *newestDataSource;
// 最热列表数据源
@property (nonatomic,strong)NSMutableArray *hotDataSource;

@end


@implementation ReadListViewController

- (UISegmentedControl *)segentedmControl {
    
    if (!_segentedmControl) {
        
        _segentedmControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        // 插入标签
        [_segentedmControl insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
        [_segentedmControl insertSegmentWithTitle:@"最热" atIndex:1 animated:YES];
        // 添加点击事件
        [_segentedmControl addTarget:self action:@selector(clickSegmentedControl:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _segentedmControl;
}

- (NSMutableArray *)newestDataSource {
    
    if (!_newestDataSource) {
        _newestDataSource = [NSMutableArray array];
    }
    return _newestDataSource;
}

- (NSMutableArray *)hotDataSource {
    
    if (!_hotDataSource) {
        _hotDataSource = [NSMutableArray array];
    }
    return _hotDataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor brownColor];
    self.navigationItem.titleView = self.segentedmControl;
    
//    [self.newestTableView registerClass:[BaseCell class] forCellReuseIdentifier:@"ReadListModel_cell"];
//    
//    [self.hotTableView registerClass:[BaseCell class] forCellReuseIdentifier:@"ReadListModel_cell"];
    
    [self.newestTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    [self.hotTableView registerNib:[UINib nibWithNibName:@"ReadListCell" bundle:nil] forCellReuseIdentifier:@"ReadListModel_cell"];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 数据请求
    [self loadNewestTableViewDataSourceWithStart:0 limit:10];
    [self loadHotTableViewDataSourceWithStart:0 limit:10];
    
    // 添加下拉刷新
    self.newestTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
       // 刷新之前清空数组
        [self.newestDataSource removeAllObjects];
        [self loadNewestTableViewDataSourceWithStart:0 limit:10];
    }];
    
    // 添加上拉加载
    self.newestTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
       
        self.newStart += 10;
        [self loadNewestTableViewDataSourceWithStart:self.newStart limit:10];
        
    }];
    // 添加下拉刷新
    self.hotTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        // 刷新之前清空数组
        [self.hotDataSource removeAllObjects];
        [self loadHotTableViewDataSourceWithStart:0 limit:10];
    }];
    
    // 添加上拉加载
    self.hotTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        
        self.hotStart += 10;
        [self loadHotTableViewDataSourceWithStart:self.hotStart limit:10];
        
    }];
}

// 分段视图控制器的点击事件
- (void)clickSegmentedControl:(UISegmentedControl *)sender {
    
    if (sender.selectedSegmentIndex == 0) {
        
        [self.scroll setContentOffset:CGPointMake(0, 0) animated:YES];
    } else {
        
        [self.scroll setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}

#pragma mark ---------数据请求-----------
// 加载最新列表的数据
- (void)loadNewestTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit {
    
    [RequestManager fecthDataWithUrl:kReadListUrl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"addtime"} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *smalldIC in bigDic[@"data"][@"list"]) {
            
            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:smalldIC];
           
            [self.newestDataSource addObject:model];
        }
    } updateUI:^{
        
        // 结束刷新
        [self.newestTableView.mj_header endRefreshing];
        // 结束加载
        [self.newestTableView.mj_footer endRefreshing];
        [self.newestTableView reloadData];
    }];
}

// 加载最热列表的数据
- (void)loadHotTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit {
    
    [RequestManager fecthDataWithUrl:kReadListUrl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"hot"} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        for (NSDictionary *smalldIC in bigDic[@"data"][@"list"]) {
            
            ReadListModel *model = [[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:smalldIC];
            [self.hotDataSource addObject:model];
        }
        
    } updateUI:^{
        
        // 结束刷新
        [self.hotTableView.mj_header endRefreshing];
        // 结束加载
        [self.hotTableView.mj_footer endRefreshing];
        
        [self.hotTableView reloadData];
    }];
}

#pragma mark ---------ScrollViewDelegate---------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 判断滚动的是不是你要滚的
    if (scrollView == self.scroll) {
        
        if (self.scroll.contentOffset.x == 0) {
            
            self.segentedmControl.selectedSegmentIndex = 0;
            
        } else if (self.scroll.contentOffset.x == kWidth) {
            
            self.segentedmControl.selectedSegmentIndex = 1;
        }
    }
}

#pragma  mark ----------tableViewdelegate-----------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.newestTableView) {
        
        return self.newestDataSource.count;
    } else {
        return self.hotDataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 创建一个model来接收
    ReadListModel *model = [[ReadListModel alloc]init];
    
    if (tableView == self.newestTableView) {
        
        model = self.newestDataSource[indexPath.row];
    } else {
        
        model = self.hotDataSource[indexPath.row];
    }
    // 工厂模式产出来的Cell
    BaseCell *cell = [FactoryMode productCellWithModel:model tableView:tableView indexPath:indexPath];
//    cell.textLabel.text = model.title;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 200;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ReadListInfoViewController *read = [[ReadListInfoViewController alloc]init];
    
    if (tableView == self.newestTableView) {
        
        read.readListModel = self.newestDataSource[indexPath.row];
    } else {
        
        read.readListModel = self.hotDataSource[indexPath.row];
    }
    
    [self.navigationController pushViewController:read animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
